﻿namespace ztymodbus
{
    public static class XB_Class
    {

        /// <summary>
        /// 将字符串转换为Double类型，如果转换失败则返回0 
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static double ToDouble(this string Value)
        {
            double ResultNum;
            bool _i = double.TryParse(Value.ToString(), out ResultNum);
            return ResultNum;
        }
        public static double ToDouble(this int Value)
        {
            double ResultNum;
            bool _i = double.TryParse(Value.ToString(), out ResultNum);
            return ResultNum;
        }

        /// <summary>
        /// 将字符串转换为int类型，如果转换失败则返回0 
        /// </summary>
        /// <param name="Value">填写需要转换的字符串</param>
        /// <param name="Default">当转换失败时，默认值为0.</param>
        /// <returns></returns>
        public static int ToInt(this string Value, int Default = 0)
        {
            int ResultNum;
            try
            {
                ResultNum = int.Parse(Value);
            }
            catch
            {
                ResultNum = Default;
            }
            //bool _i = int.TryParse(Value.ToString(), out ResultNum);
            return ResultNum;
        }

        /// <summary>
        /// 将字符串转换为long类型，如果转换失败则返回0 
        /// </summary>
        /// <param name="Value">填写需要转换的字符串</param>
        /// <param name="Default">当转换失败时，默认值为0.</param>
        /// <returns></returns>
        public static long ToLong(this string Value, long Default = 0)
        {
            long ResultNum;
            try
            {
                ResultNum = long.Parse(Value);
            }
            catch
            {
                ResultNum = Default;
            }
            //bool _i = int.TryParse(Value.ToString(), out ResultNum);
            return ResultNum;
        }

       

        public static DateTime ToDateTime(this string Value)
        {

            DateTime dtDate = DateTime.Parse("2000-01-01");
            if (DateTime.TryParse(Value.ToString(), out dtDate))
            {
                //Console.WriteLine(dtDate); 就是日期格式
            }
            else
            {
            }

            return dtDate;
        }
        /// <summary>
        /// 日期转换为时间戳（时间戳单位秒）
        /// </summary>
        /// <param name="TimeStamp"></param>
        /// <returns></returns>
        public static long ConvertToTimeStamp(this DateTime time)
        {
            DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            return (long)(time.AddHours(-8) - Jan1st1970).TotalMilliseconds;
        }

        /// <summary>
        /// 时间戳转换为日期（时间戳单位秒）
        /// </summary>
        /// <param name="TimeStamp"></param>
        /// <returns></returns>
        public static DateTime ConvertToDateTime(this long timeStamp)
        {
            var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            return start.AddMilliseconds(timeStamp).AddHours(8);
        }

    }
}
